package cn.gwm.flink.streaming.strategy.vehiclepredict;

import cn.gwm.flink.streaming.constant.ChargePredictFields;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.functions.RichMapFunction;

import java.util.List;

/**
 * Author : GW00280745
 * Date : 2022/5/25 13:19
 * Description :
 *
 * @author GW00280745
 */
public class Data2HiveFormat extends RichMapFunction {
    private List<Object> keyListResult;
    public Data2HiveFormat(List<Object> keyListResult){
        this.keyListResult = keyListResult;
    }
    @Override
    public Object map(Object value) throws Exception {
        JSONObject line = (JSONObject) value;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < keyListResult.size(); i++) {
            Object o = keyListResult.get(i);
            String key = o.toString();
            String val = val(line, key);
            stringBuilder.append(val).append(ChargePredictFields.ins().charTab);
//            stringBuilder.append(val).append(ChargePredictFields.ins().comma)
        }
        //分区键
        Long itemTime = line.getLong("item_time");
        String date = DateFormatUtils.format(itemTime, ChargePredictFields.ins().dtYyyyMmdd);
        stringBuilder.append(date);
        return stringBuilder.toString();
    }
    public String val(JSONObject line,String field){
        String string = line.getString(field);
        if (StringUtils.isBlank(string)){
            return ChargePredictFields.ins().blankStr;
        }
        return string;
    }
}
